const dateFormat = function(fmt, date) {  //时间格式化
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}

const record_play = function(item,type){  //记录点击资源
       if(type == 3 || type ==4 ){
         return
       }
       let c_id = ''
       let c_type = ''
       if(type ==1){
          c_id = item.id
          c_type = 'courseware'
       }
       if(type == 2){
          c_id = item.resMetaInfoVO.meta_id
          c_type = item.resMetaInfoVO.meta_type
       }
       if(type ==5){
          c_id = item.meta_id
          c_type = item.meta_type
       }
       $Vue.$http.post('/usermeta/createUserResRecord',{
          metaInfoId:c_id,
          metaType:c_type,
          lessonId:this.course_checked,
          chapterId:this.chapterid_checked,
          bookId:localStorage.book_id,
          publisherId:localStorage.publisher_id,
       })
       .then((res)=>{

       })
       .catch((err)=>{

       })
}

const save_play = function(item,type){  //点击资源保存本地
   if(type == 3 || type ==4 ){
     return
   }
   let c_id = ''
   let c_type = ''
   if(type ==1){
      c_id = item.id
      c_type = 'courseware'
   }
   if(type == 2){
      c_id = item.resMetaInfoVO.meta_id
      c_type = item.resMetaInfoVO.meta_type
   }
   if(type ==5){
      c_id = item.meta_id
      c_type = item.meta_type
   }
   let c_obj = {
      metaInfoId:c_id,
      metaType:c_type,
      lessonId:localStorage.course_id,
      chapterId:localStorage.chapter_id,
      bookId:localStorage.book_id,
      publisherId:localStorage.publisher_id,
      createTime:dateFormat("YYYY-mm-dd HH:MM:SS", new Date() )
   }
   window.$Vue.$db_sdk.insert('pcDB','clickrecord',c_obj)
}

export default{
	dateFormat:dateFormat,
	record_play:record_play,
	save_play:save_play,
}




